﻿
using System;
using System.Collections.Generic;


namespace CrazyMan.TM.Interface.Domain
{
    /// <summary>
    /// Represents a poker tournament.
    /// </summary>
    public class Tournament
    {
        private BettingSchedule _bettingSchedule;
        private List<Table> _tables;
        private ChipCase _chipCase;

        /// <summary>
        /// Creates a new tournament.
        /// </summary>
        public Tournament()
        {
            //create an empty betting schedule, table list and chip case
            _bettingSchedule = new BettingSchedule();
            _tables = new List<Table>();
            _chipCase = new ChipCase();
        }

        /// <summary>
        /// Creates a new tournament with a specified number of tables.
        /// </summary>
        /// <param name="tableCount">The number of empty tables to add.</param>
        public Tournament(int tableCount) : this()
        {
            //ensure valid number of tables
            ValidateTableCount(tableCount);

            AddTables(tableCount);
        }

        /// <summary>
        /// Gets and sets the betting schedule of the tournament.
        /// </summary>
        public BettingSchedule Schedule
        {
            get { return this._bettingSchedule; }
            set 
            {
                if (value == null)
                    throw new ArgumentNullException("value", "The betting schedule of a tournament can not be null.");
                
                this._bettingSchedule = value;
            }
        }

        /// <summary>
        /// Gets a list of tables in the tournament.
        /// </summary>
        public List<Table> Tables
        {
            get { return this._tables; }
        }

        /// <summary>
        /// Gets and sets the chip case that will be used for the tournament.
        /// </summary>
        public ChipCase ChipCase
        {
            get { return _chipCase; }
            set 
            {
                if (value == null)
                    throw new ArgumentNullException("value", "Can not use a null chip case in a tournament.");

                _chipCase = value; 
            }
        }

        /// <summary>
        /// Adds the specified number of table to a tournament.
        /// </summary>
        /// <param name="tableCount">The number of tables to be added to the tournament.</param>
        protected void AddTables(int tableCount)
        {
            for (int i = 0; i < tableCount; i++)
            {
                Table newTable = new Table();
                this.Tables.Add(newTable);
            }
        }

        /// <summary>
        /// Validates that the specified table count is legal.
        /// </summary>
        /// <param name="tableCount">The number of tables checked.</param>
        protected void ValidateTableCount(int tableCount)
        {
            if (tableCount < 0)
                throw new ArgumentOutOfRangeException("tableCount", "Table count should not be negative.");
        }
    }
}
